<template>

</template>

<script>
import { post } from "@/assets/js/http.js";

export default {
    name: "admin-edit",
    props: {
        selection: {
            type: Array,
            default: () => []
        },
        roleData: {
            type: Array,
            default: () => []
        },
        department: {
            type: Array,
            default: () => []
        }
    },
    components: {
        "el-upload-image": () => import("@/components/el-upload-image.vue")
    },
    watch: {
        selection() {
            this.setFormData();
        }
    },
    mounted() {
        this.setFormData();
    },
    data() {
        return {
            current: 0,
            props: {
                label: "name",
                value: "id",
                children: "children",
                multiple: false,
                checkStrictly: true
            },
            formData: {
                avatar: [],
                username: "",
                password: "",
                nickname: "",
                email: "",
                mobile: "",
            },
            formRules: {
                username: [
                    {
                        required: true,
                        message: "此项为必填项"
                    },
                    {
                        pattern: /^[A-Za-z0-9]{3,15}$/,
                        message: "格式错误"
                    }
                ],
                password: [
                    {
                        pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>/?]).{10,32}$/,
                        message: "格式错误"
                    }
                ],
                nickname: {
                    required: true,
                    message: "此项为必填项"
                },

                email: {
                    pattern: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
                    message: "邮箱格式错误",
                    trigger: "blur"
                },
                mobile: {
                    pattern: /^0?1[3|4|5|6|7|8][0-9]\d{8}$/,
                    message: "手机号格式错误",
                    trigger: "blur"
                },

            }
        };
    },
    methods: {
        setFormData() {
            if (this.selection.length > 0) {
                const data = this.selection[this.current];
                this.formData.id = data.id;
                this.formData.username = data.username;
                this.formData.nickname = data.nickname;
                this.formData.department = data.department
                this.formData.pid = data.pid
                this.formData.email = data.email;
                this.formData.mobile = data.mobile;
                this.formData.avatar = [{ url: data.avatar, name: data.avatar }];
            }
        },
        onConfirm() {
            this.$refs.formRef.validate(async isValid => {
                if (isValid) {
                    const formData = JSON.parse(JSON.stringify(this.formData));
                    // formData.group_arr = formData.rule_arr.map(item => {
                    //     console.log(item[item.length - 1]);
                    //     return item[item.length - 1];
                    // });
                    formData.group_arr = [formData.rule_arr[formData.rule_arr.length - 1]]
                    if (formData.avatar.length > 0) {
                        if (formData.avatar[0].response) {
                            formData.avatar = "http://nhcj.app65.cn/" + formData.avatar[0].response.data.file.url;
                        } else {
                            formData.avatar = formData.avatar[0].url;
                        }
                    } else {
                        formData.avatar = "";
                    }
                    await post({
                        url: "/admin/auth.admin/edit",
                        data: formData,
                        loading: true,
                        successTip: true
                    });
                    if (this.current === this.selection.length - 1) {
                        this.$emit("update");
                        this.$parent.$emit("update:visible", false);
                        this.$refs.formRef.resetFields();
                    } else {
                        this.current += 1;
                        this.setFormData();
                    }
                }
            });
        },
        onCancel() {
            if (this.current > 0) {
                this.$emit("update");
            }
            this.$parent.$emit("update:visible", false);
            this.$refs.formRef.resetFields();
        }
    }
};
</script>
